/*
    Copyright (c) Guangzhou Data Control Network Technology Co., Ltd. All rights reserved.
    Licensed under the MIT License.
    function: 动画弹窗的js
    contact:dcdev_founder@foxmail.com
*/
function popup_sidebar() {
    popup_sidebar.prototype.init = function (data) {
        var that = this;
        this.data = data;
        window["_dc_popup_sidebar_data_" + that.data.id] = data;
        //判断弹窗方向并设置动画
        switch (data.direction) {
            case 'left':
                var open = "popuptoleft-ani";
                var close = "closetoleft-ani";
                break;
            case 'right':
                var open = "popuptoright-ani";
                var close = "closetoright-ani";
                break;
            case 'bottom':
                var open = "popuptotop-ani";
                var close = "closetotop-ani";
                break;
            case 'top':
                var open = "popuptobottom-ani";
                var close = "closetobottom-ani";
                break;
            default:
                break;
        }
        // 设置默认值
        if (data.width == undefined) {
            data.width = '100%';
        }
        if (data.height == undefined) {
            data.height = '40vh';
        }
        if (data.style == undefined) {
            data.style = 'background-color: #000;opacity: 0.9;color: #fff;';
        }

        $("#" + that.data.id).prepend(`
<div class="popup popup_${that.data.id}" data-openani='${open}' data-closeani='${close}'
    style="${data.style} position: fixed;bottom: -${data.height};height: ${data.height};width: ${data.width};">
    <i id="_dc_close_popup_${that.data.id}_" style="${data.close_style}" class="close close_${that.data.id}">&#10005;</i>
    ${data.content}
</div>
`);
        //阻止弹出框的点击事件冒泡
        $(`.popup_${that.data.id}`).click(function (event) {
            event.stopPropagation();
            if (data.callback)
                data.callback.click({ html: this, id: that.data.id });
        });
        //点击空白处关闭弹窗
        $(document).click(function (event) {
            var popup = $(`.popup_${that.data.id}`),
                popup_closeani = popup.data('closeani'),
                popup_openani = popup.data('openani'),
                target = $(event.target);
            if ($(target).hasClass('popup')) {
                return false;
            }
            //如果当前弹出框是出现状态，则点击后触发弹框返回
            if (popup.hasClass(popup_openani)) {
                data.callback.click_out_popup_sidebar({ html: popup, id: that.data.id });
                popup.removeClass(popup_openani).addClass(popup_closeani);
            }
        });
        //弹出框的关闭按钮事件

        // 点击关闭按钮
        $(`.close_${that.data.id}`).on('click', function (event) {
            // 获取待关闭弹窗的id
            var id = that.data.id
            //阻止冒泡
            event.stopPropagation();
            var popup = $(`.popup_${id}`),
                //关闭窗口
                popup_closeani = popup.data('closeani'),
                popup_openani = popup.data('openani');
            data.callback.click_close_popup_sidebar({ html: popup, id: id });
            popup.removeClass(popup_openani).addClass(popup_closeani);
        });
        return;
    };

    popup_sidebar.prototype.popup = function (cb) {
        debugger;
        var that = this;
        var popup = $(`.popup_${that.data.id}`),
            popup_closeani = popup.data('closeani'),
            popup_openani = popup.data('openani');
        //弹出窗口
        popup.removeClass(popup_openani).removeClass(popup_closeani);
        setTimeout(function () {
            popup.addClass(popup_openani);
        });
        //判断是否配置了自动关闭
        if (window["_dc_popup_sidebar_data_" + that.data.id].time != undefined) {
            clearTimeout(window["popup_" + that.data.id + "_time"]);
            window["popup_" + that.data.id + "_time"] = setTimeout(function () { that.close() }, window["_dc_popup_sidebar_data_" + that.data.id].time);
        }
        if (cb) {
            cb();
        }
    };
    popup_sidebar.prototype.close_popup = function () {
        var that = this;
        var popup = $(`.popup_${that.data.id}`),
            popup_closeani = popup.data('closeani');
        popup_openani = popup.data('openani');
        //判断弹窗是否开启，如果开启则关闭
        if (popup.hasClass(popup_openani)) {
            popup.removeClass(popup_openani).addClass(popup_closeani);
            window["_dc_popup_sidebar_data_" + that.data.id].callback.click_close_popup_sidebar({ html: popup, id: that.data.id });
        }
    };
}